import 'dart:async';

import 'package:flutter/material.dart';

OverlayEntry? overlayEntry;

class SimpleToast extends StatefulWidget {
  const SimpleToast(
      {super.key,
      required this.message,
      this.theme,
      this.elevation = 5,
      this.bgColor = Colors.white,
      this.textColor = Colors.black,
      this.showDuration = const Duration(milliseconds: 200),
      this.toastDuration = const Duration(milliseconds: 2500)});

  final String message;
  final double elevation;
  final Duration showDuration;
  final Duration toastDuration;
  final Color? theme;
  final Color bgColor;
  final Color textColor;

  void show(BuildContext context) {
    showOverlay(Overlay.of(context));
  }

  void showOverlay(OverlayState? overlay) {
    if (overlay == null) {
      return;
    }

    // 只显示一个
    if (overlayEntry != null) {
      overlayEntry?.remove();
    }
    overlayEntry = OverlayEntry(builder: (context) {
      //外层使用Positioned进行定位，控制在Overlay中的位置
      return this;
    });
    //往Overlay中插入插入OverlayEntry
    overlay.insert(overlayEntry!);
  }

  @override
  State<SimpleToast> createState() => _SimpleToastState();
}

class _SimpleToastState extends State<SimpleToast>
    with TickerProviderStateMixin {
  late AnimationController controller;
  Timer? timer;

  late Color bgColor;
  late Color textColor;

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(duration: widget.showDuration, vsync: this);

    controller.forward();

    bgColor = widget.bgColor;
    textColor = widget.textColor;

    if(widget.theme!=null){
      var hsv = HSVColor.fromColor(widget.theme!);
      bgColor = hsv.withSaturation(hsv.saturation*.2).toColor();
      textColor = widget.theme!;
    }

    timer = Timer(
      widget.toastDuration,
      () {
        timer = null;
        controller.reverse();
        timer = Timer(
          widget.showDuration,
          () {
            timer = null;
            overlayEntry?.remove();
            overlayEntry = null;
          },
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: 0,
      bottom: 0,
      child: Material(
        type: MaterialType.transparency,
        child: SizedBox(
          width: MediaQuery.of(context).size.width,
          child: Center(
              child: animationBox(
            Card(
              color: bgColor,
              elevation: widget.elevation,
              shadowColor: Colors.black.withValues(alpha: .3),
              child: Container(
                constraints: BoxConstraints(maxWidth: 250),
                padding: EdgeInsets.all(8),
                child: Text(
                  widget.message,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: textColor
                  ),
                ),
              ),
            ),
          )),
        ),
      ),
    );
  }

  Widget animationBox(child) {
    return FadeTransition(
      opacity: Tween(begin: 0.0, end: 1.0).animate(controller),
      child: ScaleTransition(
        scale: Tween(begin: 0.5, end: 1.0).animate(
            CurvedAnimation(parent: controller, curve: Curves.easeOutBack)),
        child: child,
      ),
    );
  }

  @override
  void dispose() {
    if (timer != null) {
      timer?.cancel();
    }
    controller.dispose();
    super.dispose();
  }
}

// class Toast {
//   static void success(BuildContext context, String text) {
//     showToast(context, Color.fromARGB(255, 16, 255, 24), Icons.check, text);
//   }

//   static void info(BuildContext context, String text) {
//     showToast(context, Colors.blue, Icons.info, text);
//   }

//   static void warning(BuildContext context, String text) {
//     showToast(context, Colors.orange, Icons.warning, text);
//   }

//   static void error(BuildContext context, String text) {
//     showToast(context, Colors.red, Icons.clear, text);
//   }

//   static void showToast(
//       BuildContext context, Color color, IconData icon, String text) {
//     var bgColor = HSVColor.fromColor(color);
//     DelightToastBar(
//       autoDismiss: true,
//       animationDuration: const Duration(milliseconds: 200),
//       snackbarDuration: Durations.extralong4 * 2,
//       animationCurve: Curves.ease,
//       builder: (context) => ToastCard(
//         color:  bgColor.withSaturation(bgColor.saturation*.2).toColor(),
//         leading: Icon(
//           icon,
//           color: color,
//         ),
//         title: Text(
//           "hello",
//           style: TextStyle(
//             fontWeight: FontWeight.bold,
//             color: color,
//             fontSize: 14,
//           ),
//         ),
//       ),
//     ).show(context);
//   }
// }
